package com.example.hotel.controller;

import com.example.hotel.entity.ResultModel;
import com.example.hotel.entity.RoomType;
import com.example.hotel.service.RoomTypeService;
import com.example.hotel.utils.ResultUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

import static com.example.hotel.utils.CommonUtils.*;

/**
 * (RoomType)表控制层
 */
@RestController
@RequestMapping("roomType")
public class RoomTypeController {
    /**
     * 服务对象
     */
    @Resource
    private RoomTypeService roomTypeService;

    /**
     * 调用 ResultUtils
     */
    private final ResultUtils resultUtils = new ResultUtils();

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public RoomType selectOne(Integer id) {
        return this.roomTypeService.queryById(id);
    }

    /**
     * 添加
     */
    @RequestMapping(value = "insertRoomType", method = RequestMethod.POST)
    public ResultModel insertRoomType(RoomType roomType) {
        return resultUtils.InsertResultUtils(roomType, roomTypeService.insert(roomType));
    }

    /**
     * 首先查询添加的内容是否存在
     * 若不存在则进行添加操作
     */
    @RequestMapping(value = "insertnoexistRoomType", method = RequestMethod.POST)
    public ResultModel insertnoexistRoomType(RoomType roomType) {
        ResultModel resultModel = new ResultModel();//new实体对象
        try {
            //queryByName后面指定要查询的字段
            RoomType existroomType = roomTypeService.queryByName(roomType);
            if (existroomType != null) {
                ResultShow(resultModel, CODETWO, EXISTEDUSER, new HashMap<>());
            } else {
                roomType = roomTypeService.insert(roomType);
                ResultShow(resultModel, CODEONE, INSERTSUCCESS, roomType);
            }
        } catch (Exception e) {
            ResultShow(resultModel, CODEMINUSONE, INSERTFAIL, new HashMap<>());
        }
        return resultModel;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "deleteRoomType", method = RequestMethod.POST)
    public ResultModel deleteRoomType(Integer id) {
        return resultUtils.DeleteResultUtils(roomTypeService.deleteById(id));
    }

    /**
     * 修改
     */
    @RequestMapping(value = "updateRoomType", method = RequestMethod.POST)
    public ResultModel updateRoomType(RoomType roomType) {
        return resultUtils.UpdateResultUtils(roomType, roomTypeService.update(roomType));
    }

    /**
     * 查询
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.POST)
    public ResultModel selectAll(RoomType roomType) {
        List<RoomType> roomTypeList = null;
        return resultUtils.SelectResultUtils(roomTypeList, roomTypeService.queryAll(roomType));
    }

    /**
     * 通过指定字段查询单条数据
     */
    @RequestMapping(value = "selectByName", method = RequestMethod.POST)
    public RoomType selectByName(RoomType roomType) {
        return this.roomTypeService.queryByName(roomType);
    }

}